package cn.iocoder.yudao.module.fengdong.smartelderly.controller.admin.classify;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.fengdong.smartelderly.controller.admin.classify.vo.*;
import cn.iocoder.yudao.module.fengdong.smartelderly.dal.dataobject.classify.ClassifyDO;
import cn.iocoder.yudao.module.fengdong.smartelderly.convert.classify.ClassifyConvert;
import cn.iocoder.yudao.module.fengdong.smartelderly.service.classify.ClassifyService;

@Tag(name = "管理后台 - 意见/投诉 类型")
@RestController
@RequestMapping("/fengdong.smartelderly/classify")
@Validated
public class ClassifyController {

    @Resource
    private ClassifyService classifyService;

    @PostMapping("/create")
    @Operation(summary = "创建意见/投诉 类型")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:classify:create')")
    public CommonResult<Long> createClassify(@Valid @RequestBody ClassifyCreateReqVO createReqVO) {
        return success(classifyService.createClassify(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新意见/投诉 类型")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:classify:update')")
    public CommonResult<Boolean> updateClassify(@Valid @RequestBody ClassifyUpdateReqVO updateReqVO) {
        classifyService.updateClassify(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除意见/投诉 类型")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:classify:delete')")
    public CommonResult<Boolean> deleteClassify(@RequestParam("id") Long id) {
        classifyService.deleteClassify(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得意见/投诉 类型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:classify:query')")
    public CommonResult<ClassifyRespVO> getClassify(@RequestParam("id") Long id) {
        ClassifyDO classify = classifyService.getClassify(id);
        return success(ClassifyConvert.INSTANCE.convert(classify));
    }

    @GetMapping("/list")
    @Operation(summary = "获得意见/投诉 类型列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:classify:query')")
    public CommonResult<List<ClassifyRespVO>> getClassifyList(@RequestParam("ids") Collection<Long> ids) {
        List<ClassifyDO> list = classifyService.getClassifyList(ids);
        return success(ClassifyConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得意见/投诉 类型分页")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:classify:query')")
    public CommonResult<PageResult<ClassifyRespVO>> getClassifyPage(@Valid ClassifyPageReqVO pageVO) {
        PageResult<ClassifyDO> pageResult = classifyService.getClassifyPage(pageVO);
        return success(ClassifyConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出意见/投诉 类型 Excel")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:classify:export')")
    @OperateLog(type = EXPORT)
    public void exportClassifyExcel(@Valid ClassifyExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ClassifyDO> list = classifyService.getClassifyList(exportReqVO);
        // 导出 Excel
        List<ClassifyExcelVO> datas = ClassifyConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "意见/投诉 类型.xls", "数据", ClassifyExcelVO.class, datas);
    }

}
